<template>
    <el-card>
        <el-page-header title="用户管理" content="用户列表" icon="" />

        <el-table :data="tableData" stripe style="width: 100%">
            <el-table-column prop="username" label="用户名" width="180" />
            <el-table-column label="头像">
                <template #default="scope">
                    <div v-if="scope.row.avatar"><el-avatar :size="50"
                            :src="'http://203.6.239.220:3000' + scope.row.avatar"></el-avatar></div>
                    <div v-else>
                        <el-avatar :size="50"
                            src="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png" />
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="角色">
                <template #default="scope">
                    <el-tag v-if="scope.row.role === 0" type="success">普通用户</el-tag>
                    <el-tag v-if="scope.row.role === 1" type="danger">管理员</el-tag>
                    <el-tag v-else type="warning">编辑</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template #default="scope">
                <el-button size="small" @click="handleEdit(scope.row)">
                        编辑
                </el-button>
                <el-popconfirm title="确定删除此用户？" confirm-button-text="确定" cancel-button-text="取消" @confirm="handleDelete(scope.row)">
                    <template #reference>
                        <el-button size="small" type="danger">
                            删除
                        </el-button>
                    </template>
                </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
    </el-card>

    <el-dialog v-model="dialogFormVisible" title="编辑用户" width="50%">
        <el-form ref="userFormRef" :model="userForm" :rule="userRule" label-width="120px" class="demo-ruleForm" status-icon>
            <el-form-item label="用户名" prop="username">
                <el-input v-model="userForm.username" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input v-model="userForm.password" />
            </el-form-item>
            <el-form-item label="性别" prop="gender">
                <el-select v-model="userForm.gender" class="m-2" placeholder="Select" style="width: 100%;">
                    <el-option v-for="item in options1" :key="item.value" :label="item.label" :value="item.value"/>
                </el-select>
            </el-form-item>
            <el-form-item label="角色" prop="role">
                <el-select v-model="userForm.role" class="m-2" placeholder="Select" style="width: 100%;">
                    <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
            </el-form-item>
            <el-form-item label="个人简介" prop="introduction">
                <el-input v-model="userForm.introduction" type="textarea" />
            </el-form-item>
            <el-form-item label="头像" prop="avatar">
                <Upload :avatar="userForm.avatar" @lennoxchange="handleChange" />
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button type="warning" @click="dialogFormVisible = false">Canacel</el-button>
                <el-button type="primary" @click="handleEditConfirm">Confirm</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup>
import { ref, onMounted, reactive } from 'vue'
import { ElMessage } from 'element-plus';
import Upload from '../../components/upload/Upload.vue'
import axios from 'axios'
const tableData = ref([])
onMounted(() => {
    getTableData()
})

const getTableData = async () => {
    const res = await axios.get('/adminapi/user/list')
    tableData.value = res.data.data
}


const handleDelete = async (data) => {
    // console.log(data)
    const res = await axios.delete(`/adminapi/user/delete/${data._id}`)
    // console.log(res)
    if(res.data.ActionType==="OK"){
        ElMessage.success("删除成功")
        getTableData()
    }
}

const dialogFormVisible = ref(false)
const handleEdit = async (data) => {
    const res = await axios.get(`/adminapi/user/list/${data._id}`)
    if(res.data.ActionType === "OK"){
        Object.assign(userForm, res.data.data[0])
        dialogFormVisible.value = true
    }
}
const handleChange = (file) => {
    //由于Upload组件传递过来的file已经是raw原生数据了
    userForm.avatar = URL.createObjectURL(file)
    userForm.file = file
}

const handleEditConfirm = async () => {
    userFormRef.value.validate(async (valid) => {
        if (valid) {
            await axios.put(`/adminapi/user/list/${userForm._id}`, userForm)
            dialogFormVisible.value = false
            getTableData()
        }
    })
}

const userFormRef = ref()
const userForm = reactive({
    username: '',
    password: '',
    gender: '',
    role: '',
    introduction: '',
    avatar: '',
    file:null
})
const userRule = reactive({
    username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
    password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
    gender: [{ required: true, message: '请选择性别', trigger: 'blur' }],
    role: [{ required: true, message: '请选择角色', trigger: 'blur' }],
    introduction: [{ required: true, message: '请输入个人简介', trigger: 'blur' }],
    avatar: [{ required: true, message: '请上传头像', trigger: 'blur' }]
})
const options = [
    {
        value: 0,
        label: '普通用户'
    },
    {
        value: 1,
        label: '管理员'
    },
    {
        value: 2,
        label: '编辑'
    }
]
const options1 = [
    {
        value: 0,
        label: '保密'
    },
    {
        value: 1,
        label: '男'
    },
    {
        value: 2,
        label: '女'
    }
]
</script>

<style lang="css" scoped>
.el-table{
    margin-top: 50px;
}
.dialog-footer{
    text-align: center;
}
</style>